//package com.dfs.parent.common.feign;
//
//import feign.Client;
//import feign.Feign;
//import okhttp3.ConnectionPool;
//import okhttp3.OkHttpClient;
//import org.springframework.boot.autoconfigure.AutoConfigureBefore;
//import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
//import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
//import org.springframework.cloud.commons.httpclient.OkHttpClientConnectionPoolFactory;
//import org.springframework.cloud.commons.httpclient.OkHttpClientFactory;
//import org.springframework.cloud.openfeign.FeignAutoConfiguration;
//import org.springframework.cloud.openfeign.support.FeignHttpClientProperties;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//
//import java.util.concurrent.TimeUnit;
//
///**
// * <h1>OpenFeign 使用 OkHttp 配置类</h1>
// * */
//@Configuration
//@ConditionalOnClass(Feign.class)
//@AutoConfigureBefore(FeignAutoConfiguration.class)
//public class FeignOkHttpConfig {
//
//    @Bean
//    @ConditionalOnMissingBean({Client.class})
//    public Client feignClient(okhttp3.OkHttpClient client) {
//        return new feign.okhttp.OkHttpClient(client);
//    }
//    /**
//     * <h2>注入 OkHttp, 并自定义配置</h2>
//     * */
////    @Bean
////    public okhttp3.OkHttpClient okHttpClient() {
////        return new OkHttpClient.Builder()
////                .connectTimeout(10, TimeUnit.SECONDS)    // 设置连接超时
////                .readTimeout(10, TimeUnit.SECONDS)   // 设置读超时
////                .writeTimeout(10, TimeUnit.SECONDS)  // 设置写超时
////                .retryOnConnectionFailure(true)     // 是否自动重连
////                // 配置连接池中的最大空闲线程个数为 10, 并保持 5 分钟
////                .connectionPool(new ConnectionPool(
////                        10, 5L, TimeUnit.MINUTES))
////                .build();
////    }
//
////    @Bean
////    @ConditionalOnMissingBean({Client.class})
////    public Client feignClient(okhttp3.OkHttpClient client) {
////        return new feign.okhttp.OkHttpClient(client);
////    }
//
////    @Bean
////    @ConditionalOnMissingBean({ConnectionPool.class})
////    public ConnectionPool httpClientConnectionPool(FeignHttpClientProperties httpClientProperties, OkHttpClientConnectionPoolFactory connectionPoolFactory) {
////        Integer maxTotalConnections = httpClientProperties.getMaxConnections();
////        Long timeToLive = httpClientProperties.getTimeToLive();
////        TimeUnit ttlUnit = httpClientProperties.getTimeToLiveUnit();
////        return connectionPoolFactory.create(maxTotalConnections, timeToLive, ttlUnit);
////    }
//
////    @Bean
////    public OkHttpClient client(OkHttpClientFactory httpClientFactory, ConnectionPool connectionPool, FeignHttpClientProperties httpClientProperties) {
////        Boolean followRedirects = httpClientProperties.isFollowRedirects();
////        Integer connectTimeout = httpClientProperties.getConnectionTimeout();
////        Boolean disableSslValidation = httpClientProperties.isDisableSslValidation();
////        return httpClientFactory.createBuilder(disableSslValidation)
////                .connectTimeout((long)connectTimeout, TimeUnit.MILLISECONDS)
////                .followRedirects(followRedirects)
////                .connectionPool(connectionPool)
////                .addInterceptor(new OkHttpLogInterceptor()) // 自定义请求日志拦截器
////                .build();
////    }
//
//    @Bean
//    @ConditionalOnMissingBean({ConnectionPool.class})
//    public ConnectionPool httpClientConnectionPool(FeignHttpClientProperties httpClientProperties, OkHttpClientConnectionPoolFactory connectionPoolFactory) {
//        Integer maxTotalConnections = httpClientProperties.getMaxConnections();
//        Long timeToLive = httpClientProperties.getTimeToLive();
//        TimeUnit ttlUnit = httpClientProperties.getTimeToLiveUnit();
//        return connectionPoolFactory.create(maxTotalConnections, timeToLive, ttlUnit);
//    }
//
//    @Bean
//    public okhttp3.OkHttpClient client(OkHttpClientFactory httpClientFactory, ConnectionPool connectionPool, FeignHttpClientProperties httpClientProperties) {
//        Boolean followRedirects = httpClientProperties.isFollowRedirects();
//        Integer connectTimeout = httpClientProperties.getConnectionTimeout();
//        Boolean disableSslValidation = httpClientProperties.isDisableSslValidation();
//        return httpClientFactory.createBuilder(disableSslValidation)
//                .connectTimeout((long)connectTimeout, TimeUnit.MILLISECONDS).followRedirects(followRedirects)
//                .connectionPool(connectionPool).build();
//
//    }
//}
